#!/usr/bin/perl
# 
# Exercise 9.4
# 
# Modify Example 5-3 to report on the locations of the motifs that it finds, even if 
# motif appears multiple times in the sequence data.
#
# Answer to Exercise 9.4

use strict;
use warnings;


# Searching for motifs

# Ask the user for the filename of the file containing
# the protein sequence data, and collect it from the keyboard
print "Please type the filename of the protein sequence data: ";

my $proteinfilename = <STDIN>;

# Remove the newline from the protein filename
chomp $proteinfilename;

# open the file, or exit
unless ( open(PROTEINFILE, $proteinfilename) ) {

    print "Cannot open file \"$proteinfilename\"\n\n";
    exit;
}

# Read the protein sequence data from the file, and store it
# into the array variable @protein
my @protein = <PROTEINFILE>;

# Close the file - we've read all the data into @protein now.
close PROTEINFILE;

# Put the protein sequence data into a single string, as it's easier
# to search for a motif in a string than in an array of
# lines (what if the motif occurs over a line break?)
my $protein = join( '', @protein);

# Remove whitespace
$protein =~ s/\s//g;

# In a loop, ask the user for a motif, search for the motif,
# and report if it was found.
# Exit if no motif is entered.

my $motif = '';

do {
    print "Enter a motif to search for: ";

    $motif = <STDIN>;

    # Remove the newline at the end of $motif

    chomp $motif;

    # Look for the motif
##################################################
#
# Here is the modification to Exercise 5.3
#
##################################################
    my @locations = ();

    while( $protein =~ /$motif/g ) {

	# The pos function returns the position in the sequence following the match.
	# So subtract the length of the match (stored in the special variable
	# $&), and then add 1 (since Perl numbers the first position as 0)
	my $position = pos($protein) - length($&) +1;
	push(@locations, $position);
    }

    if ( @locations ) {

       print "I found it!\n\n";
       print "Locations: @locations\n";
 
##################################################
#
# Here ends the modification
#
##################################################

    } else {

        print "I couldn\'t find it.\n\n";
    }

# exit on an empty user input
} until ( $motif =~ /^\s*$/ );

# exit the program
exit;
